<template>
  <!--size抽屉宽度（或高度）  default 378px 和 large 736px -->
  <a-drawer
      v-model:open="visible"
      ref="employeeDetailModalRef"
      class="edit-drawer modal-fixed-bottom"
      root-class-name="root-customer-drawer"
      width="800px"
      :closable="false"
      @close="onClose"
  >
    <div class="closeable" @click="onClose">{{ $t('self.closeButton') }}</div>
    <div class="modal-header">{{ title }}</div>
    <a-form :model="formState" :rules="formRules" :label-col="labelCol" :wrapper-col="wrapperCol"
            @finish="onSubmit"
    >
      <a-form-item :label="$t('self.systemAccount.tips2')" name="roleName">
        <a-input allow-clear v-model:value="formState.roleName" placeholder="" />
      </a-form-item>
      <a-form-item :label="$t('self.systemAccount.tips4')" name="mark">
        <a-input allow-clear v-model:value="formState.mark" placeholder="" />
      </a-form-item>
      <a-form-item :label="$t('self.systemAccount.tips12')" name="status">
        <a-select v-model:value="formState.status" :options="[{ label: $t('self.systemAccount.tips13'), value: 1 },{ label: $t('self.systemAccount.tips14'), value: 0 }]" />
      </a-form-item>
      <a-form-item :label="$t('self.systemAccount.tips15')" name="orgId">
        <a-select v-model:value="formState.orgId" :options="orgOptions" :fieldNames="{label: 'orgName',value: 'id'}" />
      </a-form-item>

      <div class="base-flex base-rows base-align-items-center base-justify-content-center footer">
        <a-button type="primary" html-type="submit">{{ $t('self.submitButton') }}</a-button>
        <a-button class="base-ml10" @click="onClose">{{ $t('self.cancelButton') }}</a-button>
      </div>

    </a-form>
  </a-drawer>
</template>

<script setup>
import { computed, getCurrentInstance, onMounted, ref } from 'vue'
import { message } from 'ant-design-vue'
const { proxy } = getCurrentInstance()
const $TOOL = proxy.$TOOL
const $T = proxy.$t
const $API = proxy.$API
const emit = defineEmits(['close','ok'])

const visible = ref(false)
// 模式，add新增，show查看，edit编辑
const mode = ref('add')
const labelCol = {
  style: {
    width: '200px',
  },
};
const wrapperCol = {
  span: 16,
};
const title = ref('')
const formState = ref({
  "id": 0,
  "roleName": "",
  "mark": "",
  "orgId": null,
  "status": 1,
  "orgName": ""
})
const orgOptions = ref([])
const formRules = ref({
  roleName: [{required: true,message: $T('self.pleaseInput') + $T('self.systemAccount.tips2'),trigger: 'change'}],
  mark: [{required: true,message: $T('self.pleaseInput') + $T('self.systemAccount.tips4'),trigger: 'change'}],
  status: [{required: true,message: $T('self.pleaseInput') + $T('self.systemAccount.tips12'),trigger: 'change'}],
  orgId: [{required: true,message: $T('self.pleaseInput') + $T('self.systemAccount.tips15'),trigger: 'change'}],
})

onMounted(async () => {
  orgOptions.value = await proxy.getOrgSelectList(true)
})

const onSubmit = (values) => {
  const params = { ...formState.value }
  if(mode.value === 'add') {
    // 添加
    $API.backend.systemAccount.add.post(params).then(res => {
      message.success($T('self.message.success'))
      onReset()
      visible.value = false
      emit('ok')
    }).catch(err => {
      message.error(err.msg);
    })
  }else{
    // 修改
    $API.backend.systemAccount.edit.post(params).then(res => {
      message.success($T('self.message.success'))
      onReset()
      visible.value = false
      emit('ok')
    }).catch(err => {
      message.error(err.msg);
    })
  }
};

const onReset = () => {
  // 清空内容
  formState.value = {
    "id": 0,
    "roleName": "",
    "mark": "",
    "orgId": null,
    "status": 1,
    "orgName": ""
  }
}

const onClose = () => {
  onReset()
  visible.value = false
  emit('close')
}

const add = () => {
  mode.value = 'add'
  visible.value = true
  title.value = $T('self.systemAccount.tips11')
}

const edit = (record) => {
  mode.value = 'edit'
  visible.value = true
  formState.value = record
  title.value = $T('self.systemAccount.tips8') + '：' + formState.value.roleName
  // detail.value = record
  // $TOOL.array.cloneExistKey(formState.value, detail.value)
  // console.log(formState.value)
}

// 子组件中要通过defineExpose将方法暴露给父组件
defineExpose({
  add,
  edit,
})
</script>

<style lang="scss" scoped>
.edit-drawer {
  position: relative;
  .closeable {
    @include closeable;
  }
}
</style>
